##blocks_results_colors.r
##checks new block tests using within subject differences on color slidesrs
##RdE July 2015

rm(list = ls())

library(RColorBrewer)
library(grid)
library(png)
library(jpeg)
library(extrafont)

careful.user.check = F
manip.check = F

democrats = F
republicans = F
n.data = 5

lims = c(.1,.90)

##function for clustering standard errors
clx <-  function(fm, dfcw, cluster){
  library(sandwich)
  library(lmtest)
  M <- length(unique(cluster))
  N <- length(cluster)
  dfc <- (M/(M-1))*((N-1)/(N-fm$rank))
  u <- apply(estfun(fm),2,
             function(x) tapply(x, cluster, sum))
  vcovCL <- dfc*sandwich(fm, meat=crossprod(u)/N)*dfcw
  coeftest(fm, vcovCL) 
}


tests.matrix = matrix(nrow = 7,ncol = (n.data*2)+2)
pooled.matrix = matrix(nrow = 0, ncol = 7)

row.names(tests.matrix) = c('Dark','Light','Difference (Dark-Light)','Difference-in-Differences','T-statistic','P-value (one tailed)','N')
colnames(tests.matrix) = c('I1','S1','I2','S2','I3','S3','I4','S4','I5','S5','IP','SP')

main.tests.matrix = matrix(nrow = n.data+1, ncol = 11)


pooled.matrix = matrix(nrow = 0, ncol = 7)

covariate.matrix = matrix(nrow = 0, ncol = 6)


for(i in 1:n.data){ 
	
	if(i == 1){
		dat = read.csv('../data/color/results_10mod.csv') #2012
		good.occs = c('"farmer"','farmer','Farmer')
		
		}
	if(i == 2){
		dat = read.csv('../data/color/results_11mod.csv')
		good.occs = c('bishop','Bishop')
		}
	if(i == 3){
		dat = read.csv('../data/color/results_12mod.csv')
		good.occs = c('bishop','Bishop','BISHOP')
		}
	if(i == 4){
		dat = read.csv('../data/color/results_13mod.csv')
		good.occs = c('knight','Knight','knigth')
		}
	if(i == 5){
		dat = read.csv('../data/color/results_14mod.csv')
		good.occs = c('knight','Knight','knigth')
		}

		
	dat = dat[dat$V10 == 1,]
	dat = dat[dat$V7 == 0,]  ##eiliminate previews
	
  
	print(i)
	print(nrow(dat))
	if(careful.user.check == T){
		dat = dat[dat$occ %in% good.occs,]
		}
	if(democrats == T){
		dat = dat[dat$politics == 1,]
		}
	if(republicans == T){
		dat = dat[dat$politics == 2,]
		}
		
	if(manip.check == T){
		dat = dat[dat$dark_man == 3 & dat$light_man == 1,]
		}
	
	print(nrow(dat))

	##combine answers
	seg.darks = dat[, grep('seg_dark',colnames(dat))]
	seg.darks.sum = apply(seg.darks,1,sum,na.rm=T)

	seg.lights = dat[, grep('seg_light',colnames(dat))]
	seg.lights.sum = apply(seg.lights,1,sum,na.rm=T)
	
	int.darks = dat[, grep('int_dark',colnames(dat))]
	int.darks.sum = apply(int.darks,1,sum,na.rm=T)

	int.lights = dat[, grep('int_light',colnames(dat))]
	int.lights.sum = apply(int.lights,1,sum,na.rm=T)


	outputs = as.data.frame(cbind(seg.darks.sum,seg.lights.sum,int.darks.sum,int.lights.sum))
#	outputs = outputs[seg.darks.sum != 0,]
	outcome.t.diff = t.test((outputs$int.darks.sum - outputs$int.lights.sum) - (outputs$seg.darks.sum - outputs$seg.lights.sum))
  outputs$diff.diff = (outputs$int.darks.sum - outputs$int.lights.sum) - (outputs$seg.darks.sum - outputs$seg.lights.sum)
  fm = lm(diff.diff~1, data = outputs)



	print(apply(outputs,2,mean,na.rm=T))
	print(outcome.t.diff)

  segsDif = outputs$seg.darks.sum - outputs$seg.lights.sum 

  intsDif = outputs$int.darks.sum - outputs$int.lights.sum 

    tests.matrix[1,(i*2)-1] = mean(outputs$int.darks.sum); tests.matrix[1,i*2] = mean(outputs$seg.darks.sum)
    tests.matrix[2,(i*2)-1] = mean(outputs$int.lights.sum); tests.matrix[2,i*2] = mean(outputs$seg.lights.sum)
    tests.matrix[3,(i*2)-1] = mean(outputs$int.darks.sum) - mean(outputs$int.lights.sum); tests.matrix[3,i*2] = mean(outputs$seg.darks.sum) - mean(outputs$seg.lights.sum)
    tests.matrix[4,(i*2)-1] = outcome.t.diff$estimate
    tests.matrix[5,(i*2)-1] = outcome.t.diff$statistic; 
    tests.matrix[6,(i*2)-1] = outcome.t.diff$p.value
    tests.matrix[7,(i*2)-1] = nrow(outputs)

    ##new print matrix
    main.tests.matrix[i,1] = mean(outputs$int.darks.sum - outputs$int.lights.sum)
    main.tests.matrix[i,2] = sd(outputs$int.darks.sum - outputs$int.lights.sum)
    main.tests.matrix[i,3] = mean(outputs$seg.darks.sum - outputs$seg.lights.sum)
    main.tests.matrix[i,4] = sd(outputs$seg.darks.sum - outputs$seg.lights.sum)
    main.tests.matrix[i,5] = summary(fm)$coefficients[1,'Estimate']
    main.tests.matrix[i,6] = summary(fm)$coefficients[1,'Std. Error']
    main.tests.matrix[i,7] = summary(fm)$coefficients[1,'t value']
    main.tests.matrix[i,8] = summary(fm)$coefficients[1,'Estimate'] - 1.96*summary(fm)$coefficients[1,'Std. Error'] 
    main.tests.matrix[i,9] = summary(fm)$coefficients[1,'Estimate'] + 1.96*summary(fm)$coefficients[1,'Std. Error'] 
    main.tests.matrix[i,10] = summary(fm)$coefficients[1,'Pr(>|t|)']/2
    main.tests.matrix[i,11] = nrow(outputs)
    
    
	
	test = rep(i,length(segsDif))
	darks = cbind(int.darks.sum,seg.darks.sum)
	lights = cbind(int.lights.sum,seg.lights.sum)
	darks.lights = cbind(darks,lights)
	diffs = cbind(segsDif,intsDif)
	results = cbind(darks.lights,diffs) 
	results = cbind(results,test)
	
	pooled.matrix = rbind(pooled.matrix,results)
	
  
  covariate.dat = dat[c('age','sex')]
  ##recode age into years
  covariate.dat$age = as.numeric(as.character(covariate.dat$age))
  covariate.dat$age = 2012-covariate.dat$age
  

  covariate.matrix = rbind(covariate.matrix,covariate.dat)
  
  
	}
colnames(covariate.matrix) = c('age','gender')
	cat('combined \n')

	
dat = cbind(pooled.matrix,covariate.matrix)
dat$diff = dat$ints - dat$segs
	
	write.csv(dat,'TableA22_data.csv',row.names = F)	
	
# pen.col = ncol(tests.matrix)-1
# ul.col = ncol(tests.matrix)
# 
# ##regression pooled analysis
# all.df = as.data.frame(pooled.matrix)
# all.df$diff = all.df$ints - all.df$segs
# 
# fm = lm(diff~1, data = all.df)
# lm.test = clx(fm,1,all.df$test)
# 
# print(lm.test)
# 
# 
# tests.matrix[1,pen.col] = mean(all.df$int.darks.sum)
# tests.matrix[1,ul.col] = mean(all.df$seg.darks.sum)
# tests.matrix[2,pen.col] = mean(all.df$int.lights.sum)
# tests.matrix[2,ul.col] = mean(all.df$seg.lights.sum)
# tests.matrix[3,pen.col] = mean(all.df$intsDif)
# tests.matrix[3,ul.col] = mean(all.df$segsDif)
# tests.matrix[4,pen.col] = lm.test[,'Estimate']
# tests.matrix[5,pen.col] = lm.test[,'t value']; 
# tests.matrix[6,pen.col] = lm.test[,'Pr(>|t|)']/2
# tests.matrix[7,pen.col] = nrow(all.df)
# 
# 
# main.tests.matrix[6,1] = mean(all.df$intsDif)
# main.tests.matrix[6,2] = sd(all.df$intsDif)
# main.tests.matrix[6,3] = mean(all.df$segsDif)
# main.tests.matrix[6,4] = sd(all.df$segsDif)
# main.tests.matrix[6,5] = lm.test[,'Estimate']
# main.tests.matrix[6,6] = lm.test[,'Std. Error']
# main.tests.matrix[6,7] = lm.test[,'t value']
# main.tests.matrix[6,8] = lm.test[,'Estimate'] - (1.96 * lm.test[,'Std. Error'])
# main.tests.matrix[6,9] = lm.test[,'Estimate'] + (1.96 * lm.test[,'Std. Error'])
# main.tests.matrix[6,10] = lm.test[,'Pr(>|t|)']/2
# main.tests.matrix[6,11] = nrow(all.df)
# 
# figure.matrix = main.tests.matrix
# 
# main.tests.matrix = as.data.frame(round(main.tests.matrix,2))
# main.tests.matrix[,1] = paste(main.tests.matrix[,1], ' (',main.tests.matrix[,2],')',sep='')
# main.tests.matrix[,3] = paste(main.tests.matrix[,3], ' (',main.tests.matrix[,4],')',sep='')
# main.tests.matrix[,5] = paste(main.tests.matrix[,5], ' (',main.tests.matrix[,6],')',sep='')
# main.tests.matrix[,8] = paste('[',main.tests.matrix[,8],',',main.tests.matrix[,9],']',sep='')
# 
# main.tests.matrix[,9] = NULL
# main.tests.matrix[,6] = NULL
# main.tests.matrix[,4] = NULL
# main.tests.matrix[,2] = NULL
# 
# colnames(main.tests.matrix)= c('Integrated (SD)','Segregated (SD)','Difference (SE)', 'T value',
#                                'CI','p','N')


#print(out.table.main,
 #     file = "table_output/blocks_within.tex",
  #    floating = F,
  #    booktabs = T)


##Graphics
##multiply to put on scale below
# int.light.b =  mean(figure.matrix[,2])
# int.dark.b = mean(figure.matrix[,1])
# 
# seg.light.b =  mean(figure.matrix[,4])
# seg.dark.b =  mean(figure.matrix[,3])
# 
# int.light = int.light.b*1.29
# int.dark = int.dark.b*1.29
# seg.light = seg.light.b*1.29
# seg.dark = seg.dark.b*1.29
# 
# 
# int.diff = int.dark.b - int.light.b
# seg.diff = seg.dark.b - seg.light.b
# 
# 
# border.color = 'white'
# colCount <- 127 # number per row
# rowCount <- 10
# 
# 
# #dev.off()
# pdf('../book/text/Laboratory/Images/colorResults.pdf',
#     width = 8,
#     height = 4)
# par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
# plot( c(1,colCount), c(0,rowCount), type="n", axes=F, xlab = '', ylab = '')
# for(j in 1){
#   for(i in seq(from = 1, to = 129)){
#     print(i)
#     rect(xleft = i-1.1,
#          xright = i,
#          ybottom = 0,
#          ytop = 10,
#          col=rgb(0,0,255-i,maxColorValue = 255),
#          border = NA
#     )
#   }
# }
# 
# offset = 1
# 
# text(x = int.light-offset, y = 4,
#      'Integrated\n Light',
#      col = 'white', 
#      adj = c(1,0) )
# 
# text(x = seg.light-offset, y = 6,
#      'Segregated\n Light',
#      col = 'white', 
#      adj = c(1,0))
# 
# text(x = int.dark+offset, y = 4,
#      'Integrated\n Dark',
#      col = 'white', 
#      adj = c(0,0))
# 
# text(x = seg.dark+offset, y = 6,
#      'Segregated\n Dark',
#      col = 'white', 
#      adj = c(0,0))
# 
# text(x = mean(c(int.light,int.dark)),
#      y = 3.65,
#      col = 'white',
#      labels = round(int.diff,2)
#       )
# 
# text(x = mean(c(seg.light,seg.dark)),
#      y = 6.35,
#      col = 'white',
#      labels = round(seg.diff,2)
# )
# 
# 
# 
# lines(y = c(4,4),
#       x = c(int.light, int.dark),
#       col = 'white',
#       lwd = 3)
# 
# lines(y = c(6,6),
#       x = c(seg.light, seg.dark),
#       col = 'white',
#       lwd = 3)
# 
# 
# dev.off()
# 
# 
# pdf('../book/text_revised/Laboratory/Images/colorResults_2.pdf',
#     width = 8,
#     height = 4)
# par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
# plot( c(1,colCount), c(0,rowCount), type="n", axes=F, xlab = '', ylab = '')
# for(j in 1){
#   for(i in seq(from = 1, to = 129)){
#     print(i)
#     rect(xleft = i-1.1,
#          xright = i,
#          ybottom = 0,
#          ytop = 10,
#          col=rgb(0,0,255-i,maxColorValue = 255),
#          border = NA
#     )
#   }
# }
# 
# offset = 1
# 
# text(x = int.light-offset, y = 4,
#      'Integrated\n Light',
#      col = 'white',
#      cex = 1.25,
#      adj = c(1,0) )
# 
# text(x = seg.light-offset, y = 6,
#      'Segregated\n Light',
#      col = 'white',
#      cex = 1.25,
#      adj = c(1,0))
# 
# text(x = int.dark+offset, y = 4,
#      'Integrated\n Dark',
#      col = 'white', 
#      cex = 1.25,
#      adj = c(0,0))
# 
# text(x = seg.dark+offset, y = 6,
#      'Segregated\n Dark',
#      col = 'white', 
#      cex = 1.25,
#      adj = c(0,0))
# 
# text(x = mean(c(int.light,int.dark)),
#      y = 3.65,
#      col = 'white',
#      cex = 1.25,
#      labels = round(int.diff,2)
# )
# 
# text(x = mean(c(seg.light,seg.dark)),
#      y = 6.35,
#      col = 'white',
#      cex = 1.25,
#      labels = round(seg.diff,2)
# )
# 
# 
# 
# lines(y = c(4,4),
#       x = c(int.light, int.dark),
#       col = 'white',
#       lwd = 3)
# 
# lines(y = c(6,6),
#       x = c(seg.light, seg.dark),
#       col = 'white',
#       lwd = 3)
# 
# 
# dev.off()
# 
# 
# 
# 
# ##############
# border.color = 'white'
# colCount <- 127 # number per row
# rowCount <- 10
# offset = 1
# top = 20
# 
# sd.image = readPNG('color_graphics_book/segregated_dark_3.png')
# id.image = readPNG('color_graphics_book/random_dark_3.png')
# sl.image = readPNG('color_graphics_book/segregated_light_3.png')
# il.image = readPNG('color_graphics_book/random_light_3.png')
# 
# 
# jpeg('../book/text_revised_2/Laboratory/Images/colorResults_3.jpeg',
#     width = 2875,
#     height = 2000,
#     quality = 3000)
# par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
# plot( 0,0,
#       xlim = c(-5,colCount+5), 
#       ylim = c(5,rowCount+10), 
#       type="n", axes=F, xlab = '', ylab = '')
# for(j in 1){
#   for(i in seq(from = 1, to = 129)){
#     print(i)
#     rect(xleft = i-1.1,
#          xright = i,
#          ybottom = 5,
#          ytop =7,
#          col=rgb(255-i,0,0,maxColorValue = 255),
#          border = NA
#       )
#     }
#   }
# 
# line.place = 7.25
# arrows(x0 = -.1,
#        x1 = 129,
#       y0 = line.place,
#       y1 = line.place,
#   lwd = 15,
#   code = 3,
#   col = brewer.pal(9,'Greys')[5])
# 
# 
# 
# left.1 = 1
# left.2 = left.1 + 32
# left.3 = left.2 + 32
# left.4 = left.3 + 32
# left.5 = left.4 + 32
# square.top = top
# square.bottom = top - 5.07874
# 
# rasterImage(il.image,
#             left.1,
#             square.bottom,
#             left.2,
#             square.top)
# rasterImage(sl.image,
#             left.2,
#             square.bottom,
#             left.3,
#             square.top)
# rasterImage(sd.image,
#             left.3,
#             square.bottom,
#             left.4,
#             square.top)
# rasterImage(id.image,
#             left.4,
#             square.bottom,
#             left.5,
#             square.top)
# 
# line.offset = .25
# arrow.1.top = (left.2-left.1)/2
# arrow.2.top = left.2 + ((left.3-left.2)/2)
# arrow.3.top = left.3 + ((left.4-left.3)/2)
# arrow.4.top = left.4 + ((left.5-left.4)/2)
# 
# arrows(x0 = arrow.1.top,
#        x1 = int.light,
#        y0 = square.bottom+.75,
#        y1 = line.place+line.offset,
#        lwd = 8,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.2.top,
#        x1 = seg.light,
#        y0 = square.bottom+.75,
#        y1 = line.place+line.offset,
#        lwd = 8,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.3.top,
#        x1 = seg.dark,
#        y0 = square.bottom+.75,
#        y1 = line.place+line.offset,
#        lwd = 8,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.4.top,
#        x1 = int.dark,
#        y0 = square.bottom+.75,
#        y1 = line.place+line.offset,
#        lwd = 8,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# text(x = c(arrow.1.top,arrow.2.top,arrow.3.top,arrow.4.top),
#     y = rep(square.top-.5,4),
#    labels =  c('Integrated Light','Segregated Light','Segregated Dark','Integrated Dark'),
#     cex = 3
#   )
#     
# text(
#   x = c(int.light-1,seg.light+1,seg.dark-1,int.dark+1),
#   y = rep(line.place+line.offset,4),
#   label = round(c(int.light,seg.light,seg.dark,int.dark),2),
#   pos = c(2,4,2,4),
#   cex = 3
# )
# 
# dev.off()
# 

################3
#library(imager)
#file = load.image('../book/text_revised_2/Laboratory/Images/colorResults_3.jpeg')
#im <- load.image(file)
#grayscale(im)


#source("http://bioconductor.org/biocLite.R")
#biocLite("EBImage")
#library(EBImage)

#color.image <- readImage("../book/text_revised_2/Laboratory/Images/colorResults_3.jpeg")
#bw.image <- channel(color.image,"gray")
#writeImage(bw.image,file="bw.png")

###

# top = 70.26087
# 
# sd.image = readJPEG('color_graphics_book/segregated_dark_3.jpeg')
# id.image = readJPEG('color_graphics_book/random_dark_3.jpeg')
# sl.image = readJPEG('color_graphics_book/segregated_light_3.jpeg')
# il.image = readJPEG('color_graphics_book/random_light_3.jpeg')
# 
# 
# text.size = 1.5
# 
# jpeg('../book/text_revised_2/Laboratory/Images/colorResults_4.jpeg',
#      width = 11,
#      height = 8.5,
#      units = 'in',
#      res = 600)
# par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
# plot( 0,0,
#       xlim = c(0,100), 
#       ylim = c(0,top), 
#       type="n", axes=F, xlab = '', ylab = '')
# 
# 
# line.place = 7.25
# arrows(x0 = 5,
#        x1 = 94,
#        y0 = line.place,
#        y1 = line.place,
#        lwd = 15,
#        code = 3,
#        col = brewer.pal(9,'Greys')[5])
# 
# 
# image.border = 4
# image.width = 20
# left.1 = 4
# right.1 = 24
# left.2 = 28
# right.2 = 48
# left.3 = 52
# right.3 = 72
# left.4 = 76
# right.4 = 96
# 
# square.top = top*.8
# square.bottom = square.top-20
# 
# rasterImage(il.image,
#             left.1,
#             square.bottom,
#             right.1,
#             square.top)
# rasterImage(sl.image,
#             left.2,
#             square.bottom,
#             right.2,
#             square.top)
# rasterImage(sd.image,
#             left.3,
#             square.bottom,
#             right.3,
#             square.top)
# rasterImage(id.image,
#             left.4,
#             square.bottom,
#             right.4,
#             square.top)
# 
# line.offset = 1
# arrow.offset = 0
# arrow.1.top = left.1 + ((right.1-left.1)/2)
# arrow.2.top = left.2 + ((right.2-left.2)/2)
# arrow.3.top = left.3 + ((right.3-left.3)/2)
# arrow.4.top = left.4 + ((right.4-left.4)/2)
# 
# arrows(x0 = arrow.1.top,
#        x1 = int.light,
#        y0 = square.bottom + arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 8,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.2.top,
#        x1 = seg.light,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 8,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.3.top,
#        x1 = seg.dark,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 8,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.4.top,
#        x1 = int.dark,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 8,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# text(x = c(arrow.1.top,arrow.2.top,arrow.3.top,arrow.4.top),
#      y = rep(square.top+1),
#      labels =  c('Integrated Light','Segregated Light','Segregated Dark','Integrated Dark'),
#      cex = text.size
#       )
# 
# text(
#   x = c(int.light-1.75,seg.light+1,seg.dark-1.75,int.dark+1),
#   y = rep(line.place+1.5,4),
#   label = round(c(int.light,seg.light,seg.dark,int.dark),2),
#   pos = c(2,4,2,4),
#   cex = text.size - .25
#   )
# 
# text(
#   x = 50.5,
#   y = line.place - 1,
#   label = '"What was the color of the block?"',
#   pos = 1,
#   cex = text.size + 1
#     )
# 
# 
# text(
#   x = c(5,94),
#   y = rep(line.place,line.place),
#   label = c('Light\n(0)','Dark\n(100)'),
#   pos = c(2,4),
#   cex = text.size - .5
#   )
# 
# dev.off()
# 
# 
# ###############
# #############
# 
# top = 70.26087
# 
# sd.image = readJPEG('color_graphics_book/segregated_dark_3.jpeg')
# id.image = readJPEG('color_graphics_book/random_dark_3.jpeg')
# sl.image = readJPEG('color_graphics_book/segregated_light_3.jpeg')
# il.image = readJPEG('color_graphics_book/random_light_3.jpeg')
# 
# 
# text.size = 1.25
# 
# jpeg('../book/text_revised_2/Laboratory/Images/colorResults_5.jpeg',
#      height =  4.66,
#      width  = 7.33,
#      units = 'in',
#      res = 1200,
#      family = 'Sabon LT Std',
#      pointsize = 10)
# par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
# plot( 0,0,
#       xlim = c(0,100), 
#       ylim = c(0,top), 
#       type="n", axes=F, xlab = '', ylab = '')
# 
# 
# line.place = 7.25
# arrows(x0 = 7.5,
#        x1 = 92.5,
#        y0 = line.place,
#        y1 = line.place,
#        lwd = 10,
#        code = 3,
#        col = brewer.pal(9,'Greys')[5])
# 
# 
# image.border = 4
# image.width = 20
# left.1 = 4
# right.1 = 24
# left.2 = 28
# right.2 = 48
# left.3 = 52
# right.3 = 72
# left.4 = 76
# right.4 = 96
# 
# square.top = top*.8
# square.bottom = square.top-20
# 
# rasterImage(il.image,
#             left.1,
#             square.bottom,
#             right.1,
#             square.top)
# rasterImage(sl.image,
#             left.2,
#             square.bottom,
#             right.2,
#             square.top)
# rasterImage(sd.image,
#             left.3,
#             square.bottom,
#             right.3,
#             square.top)
# rasterImage(id.image,
#             left.4,
#             square.bottom,
#             right.4,
#             square.top)
# 
# line.offset = 2
# arrow.offset = -.75
# arrow.1.top = left.1 + ((right.1-left.1)/2)
# arrow.2.top = left.2 + ((right.2-left.2)/2)
# arrow.3.top = left.3 + ((right.3-left.3)/2)
# arrow.4.top = left.4 + ((right.4-left.4)/2)
# 
# arrows(x0 = arrow.1.top,
#        x1 = int.light,
#        y0 = square.bottom + arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 5,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.2.top,
#        x1 = seg.light,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 5,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.3.top,
#        x1 = seg.dark,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 5,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.4.top,
#        x1 = int.dark,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 5,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# text(x = c(arrow.1.top,arrow.2.top,arrow.3.top,arrow.4.top),
#      y = rep(square.top+1),
#      labels =  c('Integrated light','Segregated light','Segregated dark','Integrated dark'),
#      cex = text.size
# )
# 
# text(
#   x = c(int.light-2.6,seg.light+.3,seg.dark-2.3,int.dark+.3),
#   y = rep(line.place+2,4),
#   label = round(c(int.light,seg.light,seg.dark,int.dark),2),
#   pos = c(2,4,2,4),
#   cex = text.size - .25,
#   font = 2
#   )
# 
# text(
#   x = 50.5,
#   y = line.place - 1,
#   label = '"What was the color of the block?"',
#   pos = 1,
#   cex = text.size + .25,
#   font = 3
#   )
# 
# 
# text(
#   x = c(7.5,92.5),
#   y = rep(line.place,line.place),
#   label = c('Lighter','Darker'),
#   pos = c(2,4),
#   cex = text.size - .25
# )
# 
# dev.off()
# 
# 
# ########################################
# ###################################
# top = 70.26087
# 
# sd.image = readJPEG('color_graphics_book/segregated_dark_3.jpeg')
# id.image = readJPEG('color_graphics_book/random_dark_3.jpeg')
# sl.image = readJPEG('color_graphics_book/segregated_light_3.jpeg')
# il.image = readJPEG('color_graphics_book/random_light_3.jpeg')
# 
# 
# text.size = 1.25
# 
# tiff('../book/text_revised_2/Laboratory/Images/colorResults_5.tiff',
#      height =  4.66,
#      width  = 7.33,
#      units = 'in',
#      res = 1200,
#      family = 'Sabon LT Std',
#      pointsize = 10)
# par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
# plot( 0,0,
#       xlim = c(0,100), 
#       ylim = c(0,top), 
#       type="n", axes=F, xlab = '', ylab = '')
# 
# 
# line.place = 7.25
# arrows(x0 = 7.5,
#        x1 = 92.5,
#        y0 = line.place,
#        y1 = line.place,
#        lwd = 10,
#        code = 3,
#        col = brewer.pal(9,'Greys')[5])
# 
# 
# image.border = 4
# image.width = 20
# left.1 = 4
# right.1 = 24
# left.2 = 28
# right.2 = 48
# left.3 = 52
# right.3 = 72
# left.4 = 76
# right.4 = 96
# 
# square.top = top*.8
# square.bottom = square.top-20
# 
# rasterImage(il.image,
#             left.1,
#             square.bottom,
#             right.1,
#             square.top)
# rasterImage(sl.image,
#             left.2,
#             square.bottom,
#             right.2,
#             square.top)
# rasterImage(sd.image,
#             left.3,
#             square.bottom,
#             right.3,
#             square.top)
# rasterImage(id.image,
#             left.4,
#             square.bottom,
#             right.4,
#             square.top)
# 
# line.offset = 2
# arrow.offset = -.75
# arrow.1.top = left.1 + ((right.1-left.1)/2)
# arrow.2.top = left.2 + ((right.2-left.2)/2)
# arrow.3.top = left.3 + ((right.3-left.3)/2)
# arrow.4.top = left.4 + ((right.4-left.4)/2)
# 
# arrows(x0 = arrow.1.top,
#        x1 = int.light,
#        y0 = square.bottom + arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 5,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.2.top,
#        x1 = seg.light,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 5,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.3.top,
#        x1 = seg.dark,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 5,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# arrows(x0 = arrow.4.top,
#        x1 = int.dark,
#        y0 = square.bottom+ arrow.offset,
#        y1 = line.place+line.offset,
#        lwd = 5,
#        code = 2,
#        col =  brewer.pal(9,'Greys')[4])
# 
# text(x = c(arrow.1.top,arrow.2.top,arrow.3.top,arrow.4.top),
#      y = rep(square.top+1),
#      labels =  c('Integrated light','Segregated light','Segregated dark','Integrated dark'),
#      cex = text.size
# )
# 
# text(
#   x = c(int.light-2.6,seg.light+.3,seg.dark-2.3,int.dark+.3),
#   y = rep(line.place+2,4),
#   label = round(c(int.light,seg.light,seg.dark,int.dark),2),
#   pos = c(2,4,2,4),
#   cex = text.size - .25,
#   font = 2
# )
# 
# text(
#   x = 50.5,
#   y = line.place - 1,
#   label = '"What was the color of the block?"',
#   pos = 1,
#   cex = text.size + .25,
#   font = 3
# )
# 
# 
# text(
#   x = c(7.5,92.5),
#   y = rep(line.place,line.place),
#   label = c('Lighter','Darker'),
#   pos = c(2,4),
#   cex = text.size - .25
# )
# 
# dev.off()
# 

